package com.example.vote.flow.controller;

import com.example.vote.flow.exception.RequestParameterNotValidException;
import com.example.vote.flow.pojo.core.Principle;
import com.example.vote.flow.pojo.param.VoteTopicCreateParam;
import com.example.vote.flow.pojo.param.VoteTopicUpdateParam;
import com.example.vote.flow.pojo.vo.VoteTopicFullVO;
import com.example.vote.flow.pojo.vo.VoteTopicSimpleVO;
import com.example.vote.flow.service.IVoteTopicService;
import com.example.vote.flow.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "2. 投票主题管理模块")
@RestController
@RequestMapping("/api/vote-topic")
public class VoteTopicController {

    @Autowired
    private IVoteTopicService voteTopicService;

    @ApiOperationSupport(order = 100)
    @Operation(summary = "[用户] 创建新投票主题")
    @PostMapping("/create")
    public JsonResult<Void> create(@ParameterObject VoteTopicCreateParam param, HttpSession session) {
        // 检查开始时间是否晚于结束时间
        if (param.getStartTime().isAfter(param.getEndTime())) {
            throw new RequestParameterNotValidException("开始时间不能晚于结束时间");
        }
        // 确保每个用户每天最大投票数不能大于用户最大投票数
        if (param.getMaxVotesPerUserPerDay() > param.getMaxVotesPerUser()) {
            throw new RequestParameterNotValidException("每个用户每天最大投票数不能大于用户最大投票数");
        }
        // 确保每个用户的最大投票数不超过IP地址的最大投票数
        if (param.getMaxVotesPerUser() > param.getMaxVotesPerIp()) {
            throw new RequestParameterNotValidException("每个用户最大投票数不能大于IP最大投票数");
        }

        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        voteTopicService.create(param, principle);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 200)
    @Operation(summary = "[用户] 删除投票主题")
    @Parameter(name = "id", description = "投票主题id", required = true, example = "1")
    @PostMapping("/{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id, HttpSession session) {
        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        voteTopicService.deleteById(id, principle);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 300)
    @Operation(summary = "[用户] 编辑投票主题")
    @Parameter(name = "id", description = "投票主题id", required = true, example = "1")
    @PostMapping("/{id}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @ParameterObject VoteTopicUpdateParam param, HttpSession session) {
        Principle principle = (Principle) session.getAttribute(Principle.SESSION_KEY);
        voteTopicService.updateById(id, param, principle);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 410)
    @Operation(summary = "[游客] 根据ID查询投票主题基本信息")
    @Parameter(name = "id", description = "投票主题id", required = true, example = "1")
    @GetMapping("/public/{id}")
    public JsonResult<VoteTopicSimpleVO> selectById(@PathVariable Long id) {
        VoteTopicSimpleVO data = voteTopicService.selectSimpleInfoById(id);
        return JsonResult.ok(data);
    }

    @ApiOperationSupport(order = 411)
    @Operation(summary = "[游客] 根据ID查询投票主题详细信息")
    @Parameter(name = "id", description = "投票主题id", required = true, example = "1")
    @GetMapping("/public/{id}/full-info")
    public JsonResult<VoteTopicFullVO> selectFullById(@PathVariable Long id) {
        VoteTopicFullVO data = voteTopicService.selectFullInfoById(id);
        return JsonResult.ok(data);
    }

    @ApiOperationSupport(order = 420)
    @Operation(summary = "[游客] 查询投票主题列表")
    @GetMapping("/public")
    public JsonResult<List<VoteTopicSimpleVO>> selectAll() {
        List<VoteTopicSimpleVO> list = voteTopicService.selectAll();
        return JsonResult.ok(list);
    }

}